<?php

namespace backend\controllers;

use yii\base\Controller;
use backend\models\ReportForm;
use yii;
use yii\base\Action;
use backend\models\TmeOrderinfo;
use backend\models\TbaMemberinfo;
use backend\models\TmeMerchandiseinfo;
use backend\models\TmeUnitinfo;
use backend\models\TmeOrderdetailsinfo;
use yii\db\Query;
/**
 * #name=入库统计报表#
 * @author Administrator
 *
 */
class BgdsController extends Controller {
	public $enableCsrfValidation = false;
	
	public  $layout = 'ace';
	
	/**
	 * #operation=默认操作#
	 * @return Ambigous <string, string>
	 */
	public function actionIndex()
	{
		//实例化模型
		$model= new ReportForm(['scenario'=>'inquire']);
		
		//视图传送
		return $this->render('bgds',[
			'model'=>$model
		]);
		
	}
	
	

	
	/**
	 * #operation=表格数据展现#
	 */
	public function actionTable()
	{
		
		
		
		if (\Yii::$app->request->post('action')=='action'){
			
			$start_time=\yii::$app->request->post('start_time');  //开始时间
			$end_time=\yii::$app->request->post('end_time');     //结束时间
			
			
			//时间初始化
			if (empty($start_time) or empty($end_time)){
				$season = ceil((date('n'))/3);
				$start_time=date('Y-m', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
				$end_time=date('Y-m', mktime(23,59,59,$season*12,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')));
			
			}
		
		//判断是否起始时间是否是在同一个月
		$monthif  = floor((strtotime($start_time)-strtotime($end_time))/ 60 / 60 / 24 / 30);
		
	
	   if ($monthif == 0 or $monthif == -1){
			unset($arry);
			$start_time=date('Y-m-d',strtotime($start_time));
			$end_time=date('Y-m-d',strtotime($end_time.'23:59:59'));
			$where="intime>='".$start_time."' and intime<='".$end_time."'";
			
			
			//时间段内有值的去重后日期
			$datas = (new \yii\db\Query())
				->select(['DATE_FORMAT(intime,\'%Y-%m-%d\') month'])
				->from('tme_instockinfo')
				->where($where)
				->groupBy('month')
				->all();
				
		
				foreach ($datas as $c){
			
					$arry[]=$c['month'];
			
					//订单数量
					$show []= (new \yii\db\Query())
					->select('*')
					->from('tme_instockinfo')
					->where(['like','intime',$c['month']])
					->count();
			
					//订单金额
					$money = (new \yii\db\Query())
					->select(['TotalMoney'])
					->from('tme_instockinfo')
					->where(['like','intime',$c['month']])
					->all();
					$moneys='';
						foreach ($money as $i){
							$moneys +=$i['TotalMoney'];
				
						}
					$show1[]=$moneys;
			
			 	}
			
			}else{
		
					$where="intime>='".$start_time."' and intime<='".$end_time."'";
				
					$datas = (new \yii\db\Query())
					->select(['DATE_FORMAT(intime,\'%Y-%m\') month,sum(totalmoney),count(*) as num'])
					->from('tme_instockinfo')
					->where($where)
					->groupBy('month')
					->all();
				
					$arry=[];
					foreach($datas as $v){
						$show[]=$v['num'];
						$show1[]=$v['sum(totalmoney)'];
						$arry[]=$v['month'];
		
			 }
			
	  }	
	
 }
		if (\Yii::$app->request->post('list')=='0'){
			
			 $start=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
			 $end=date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
			
			 $where="intime>='".$start."' and intime<='".$end."'";
			 
			 $datas = (new \yii\db\Query())
			 ->select(['DATE_FORMAT(intime,\'%Y-%m-%d\') month'])
			 ->from('tme_instockinfo')
			 ->where($where)
			 ->groupBy('month')
			 ->all();
			 
			 $arry=[];
			 foreach($datas as $v){
			 	//订单时间
			 	$times=$v['month'];
			 	
			 	//订单总个数
			 	$show []=(new \yii\db\Query())
			 	->select('*')
			 	->from('tme_instockinfo')
			 	->where(['DATE_FORMAT(intime,\'%Y-%m-%d\')'=>$times])
			 	->count();
			 		
			 	//订单总金额
			 	$mony = (new \yii\db\Query())
			 	->select(['TotalMoney'])
			 	->from('tme_instockinfo')
			 	->where(['DATE_FORMAT(intime,\'%Y-%m-%d\')'=>$times])
			 	->all();
			 	$monys='';
			 	foreach ($mony as $i){
			 		$monys +=$i['TotalMoney'];
			 	}
			 	$arry[]=$times;
			 	$show1[]=$monys;
			 	
			 	
			 
			 }
			
			
		}	
	
		if (\Yii::$app->request->post('list')=='1'){
		
			$start=date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
			$end=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
		
			$where="intime>='".$start."' and intime<='".$end."'";
			 
			 $datas = (new \yii\db\Query())
			 ->select(['DATE_FORMAT(intime,\'%Y-%m-%d\') month'])
			 ->from('tme_instockinfo')
			 ->where($where)
			 ->groupBy('month')
			 ->all();
			 
			 $arry=[];
			 foreach($datas as $v){
			 	//订单时间
			 	$times=$v['month'];
			 	
			 	//订单总个数
			 	$show []=(new \yii\db\Query())
			 	->select('*')
			 	->from('tme_instockinfo')
			 	->where(['DATE_FORMAT(intime,\'%Y-%m-%d\')'=>$times])
			 	->count();
			 		
			 	//订单总金额
			 	$mony = (new \yii\db\Query())
			 	->select(['TotalMoney'])
			 	->from('tme_instockinfo')
			 	->where(['DATE_FORMAT(intime,\'%Y-%m-%d\')'=>$times])
			 	->all();
			 	$monys='';
			 	foreach ($mony as $i){
			 		$monys +=$i['TotalMoney'];
			 	}
			 	$arry[]=$times;
			 	$show1[]=$monys;
			 	
			 	
			 
			 }
		}
	
		if (\Yii::$app->request->post('list')=='2'){
				$year=date("Y",time());
				$start=$year."-01-01";
				$end=$year."-12-31";
				
				$where="intime>='".$start."' and intime<='".$end."'";
					
				$datas = (new \yii\db\Query())
				->select(['DATE_FORMAT(intime,\'%Y-%m\') month,sum(totalmoney),count(*) as num'])
				->from('tme_instockinfo')
				->where($where)
				->groupBy('month')
				->all();
				
				$arry=[];
				foreach($datas as $v){
					$show[]=$v['num'];
					$show1[]=$v['sum(totalmoney)'];
					$arry[]=$v['month'];
			
				}
		}
	
		
		//没有数据的提示信息
		if(empty($show) and empty($show1)){
				$show[]='';
				$show1[]='';
				$arry[]='您搜索的时间段没有订单!';
		}
	
				$arry=array_values($arry);
				$title=['订单数','订单金额'];
				$monent=$arry;
				$names=[
					['type'=>'value','name'=>$title[0],'axisLabel'=>['formatter'=>'{value} 个']],
					['type'=>'value','name'=>$title[1],'axisLabel'=>['formatter'=>'{value} 元']]
				];
					//订单数组展现
					$arry1=$show;
					//订单金额数组展现
					$arry2=$show1;
				$series=[
					['name'=>$title[0],'type'=>'bar','data'=>$arry1],
					['name'=>$title[1],'type'=>'line','yAxisIndex'=>1,'data'=>$arry2]
				];
				$aa=[
					$title,
					$monent,
					$names,
					$series
				];
				
				echo  json_encode($aa);  //将数据转变为json字符串
	
	}
	
	

	/**
	 * #operation=订单统计数据查询#
	 */
	
	//json数据   订单统计数据查询
	public function actionAjxapie(){
		
			//if(\yii::$app->request->isPost){
			 
			$start=intval(\yii::$app->request->post('start'));    //起始数据条数
			$length=intval(\yii::$app->request->post('length'));  //需要显示的数据条数
			$draw=intval(\yii::$app->request->post('draw'));
		
			$column=intval(\yii::$app->request->post('order')[0]['column']);   //排序的列
			$dir = \yii::$app->request->post('order')[0]['dir'];     //排序方式
			$start_time=\yii::$app->request->post('start_time');  //开始时间
		   $end_time=\yii::$app->request->post('end_time');     //结束时间
		   
		   
		   
		   if (\Yii::$app->request->post('list')=='2'){
		   	$year=date("Y",time());
		   	$start_time=$year."-01-01";
		   	$end_time=$year."-12-31";
		  
		   }
		   if (\Yii::$app->request->post('list')=='1'){
		  
		   	$start_time=date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
		   	$end_time=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
		   	
		   
		   }
		   if (\Yii::$app->request->post('list')=='0'){
		   
		   	$start_time=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
		   	$end_time=date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
		   
		   	 
		   }
		   
		   
			if ($start_time!=0 or $end_time!=0){
				$where1 ="intime>='".$start_time."' and intime<='".$end_time."'";
				
			}else{
				$where1='';
			} 
		
		
		
			
			$columArry=[
		
			"0"=>"BillCode",
			"1"=>"RecMan",
			"2"=>"DeliveryName",
			"3"=>"TotalMoney"
			];
		
		
			//构建排序条件
			
				$cloumName=$columArry[$column];
			
			
			//根据搜索值时构建查询条件
			if(\yii::$app->request->post('search')['value']!=""){
		
				$value=\yii::$app->request->post('search')['value'];
				
				
					$where = "intime like '%".$value."%'";
		
			}else{
		
				$where="";
			}
		
			
			//根据条件查询数据
			
			$report = (new \yii\db\Query())
			->select(['DATE_FORMAT(intime,"%Y-%m-%d") month'])
			->from('tme_instockinfo')
			->where($where1)			
			->andWhere($where)
			->orderBy($cloumName." ".$dir)
			->limit($length)
			->offset($start)
			->all();
			
			
			
			$total =count($report);      //查询数据总数
			
			$arr=[];
			foreach ($report as $v){
					
			//订单时间	
			$times=$v['month'];	
			 
			//订单总个数	
			$totale=(new \yii\db\Query())
			->select('*')
			->from('tme_instockinfo')
			->where(['intime'=>$times])
			->count();
			
			//订单总金额
			$mony = (new \yii\db\Query())
			->select(['TotalMoney'])
			->from('tme_instockinfo')
			->where(['intime'=>$times])
			->all();
			$monys='';
			foreach ($mony as $i){
				$monys +=$i['TotalMoney'];
			}
			
			//待审核订单
			
			$notorder=(new \yii\db\Query())
			->select('BillStatus')
			->from('tme_instockinfo')
			->where(['intime'=>$times])
			->andWhere(['intype'=>'0'])
			->count();
			
			
				
				 
				$temp=[];
				
				$temp['BillCode']=$times;
				$temp['RecMan']=$totale;
				$temp['Delivery']=$monys;
				$temp['TotalMoney']=$notorder;
				
				
				 
				$arr[]=$temp;
		
			}
			 
			//构造返回数据
			$tmp=[
			"draw"=>$draw,
			"recordsTotal"=>$total,     //数据综述
			"recordsFiltered"=>$total,  //数据总数
			"data"=>$arr                //数据
			 
			];
			 
			echo  json_encode($tmp);  //将数据转变为json字符串
		
		}
		
	
		
	//}
}

?>